package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.*;
import com.example.domain.model.MovieDTO;
import com.example.domain.model.SearchDTO;
import com.example.mapper.*;
import com.example.service.MovieService;
import com.example.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* @author Administrator
* @description 针对表【movie】的数据库操作Service实现
* @createDate 2023-03-20 09:44:37
*/
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie>implements MovieService{

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private ActorMapper actorMapper;

    @Autowired
    private ActorMovieMapper actorMovieMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RatingMapper ratingMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private MovieCategoryMapper movieCategoryMapper;

    @Override
    public MovieDTO getById(String id) {
        return movieMapper.selectInfo(id);
    }

    @Override
    public IPage<MovieDTO> getList(Page<MovieDTO> page, SearchDTO searchDTO) {
        page.setOptimizeCountSql(false);
        return movieMapper.selectPageList(page,null,searchDTO.getSearchKeyword());
    }

    @Override
    public IPage<MovieDTO> getNewList(Page<Movie> page) {
        page.setOptimizeCountSql(false);
        return movieMapper.selectNewPage(page);
    }

    @Override
    public IPage<MovieDTO> getHotList(Page<Movie> page) {
        page.setOptimizeCountSql(false);
        return movieMapper.selectHotPage(page);
    }


    @Override
    public boolean save(MovieDTO movieDTO) {

        List<String> actorList = movieDTO.getActorsList();
        List<String> actorImageList = movieDTO.getActorImageList();
        Movie movie = JsonUtil.getJsonToBean(movieDTO, Movie.class);

        movieMapper.insert(movie);

        for (String actorName : actorList){               //遍历演员列表

            Actor actor = actorMapper.selectOne(new LambdaQueryWrapper<Actor>()
                    .eq(Actor::getName, actorName));

            ActorMovie actorMovie = new ActorMovie();
            actorMovie.setMovieId(movie.getId());

            if (actor == null){                 //演员是否已存在  已存在->新增关联表  不存在->新增演员->新增关联表
                Actor actor1 = new Actor();
                actor1.setName(actorName);
                actor1.setImageUrl(actorImageList.get(actorList.indexOf(actorName)));
                actorMapper.insert(actor1);
                actorMovie.setActorId(actor1.getId());
            }else {
                actorMovie.setActorId(actor.getId());
            }
            actorMovieMapper.insert(actorMovie);
        }

        List<String> categoryList = movieDTO.getCategoryList();

        for (String categoryName : categoryList){
            Category category = categoryMapper.selectOne(
                    new LambdaQueryWrapper<Category>()
                            .eq(Category::getName, categoryName));
            MovieCategory movieCategory = new MovieCategory();
            movieCategory.setMovieId(movie.getId());

            movieCategory.setCategoryId(category.getId());
            movieCategoryMapper.insert(movieCategory);
        }

        return true;
    }

    @Transactional
    @Override
    public boolean update(String id, MovieDTO movieDTO) {
        Movie m = movieMapper.selectById(id);

        if (m != null){
            //修改电影表
            Movie movie = JsonUtil.getJsonToBean(movieDTO, Movie.class);
            movie.setId(id);
            movieMapper.updateById(movie);

            //修改电影——分类表
            movieCategoryMapper.delete(new LambdaQueryWrapper<MovieCategory>()
                    .eq(MovieCategory::getMovieId,id));
            List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .in(Category::getName, movieDTO.getCategoryList()));
            for (Category category:categoryList){
                movieCategoryMapper.insert(new MovieCategory(id,category.getId()));
            }

            //修改演员——电影表
            actorMovieMapper.delete(new LambdaQueryWrapper<ActorMovie>()
                    .eq(ActorMovie::getMovieId,id));
            List<Actor> actorList = actorMapper.selectList(new LambdaQueryWrapper<Actor>()
                    .in(Actor::getName, movieDTO.getActorsList()));
            for (Actor actor:actorList){
                actorMovieMapper.insert(new ActorMovie(id,actor.getId()));
            }
            return true;
        }
        return false;

    }

    @Override
    public boolean delete(String id) {
        movieCategoryMapper.delete(new LambdaQueryWrapper<MovieCategory>().eq(MovieCategory::getMovieId,id));
        favoriteMapper.delete(new LambdaQueryWrapper<Favorite>().eq(Favorite::getMovieId,id));
        actorMovieMapper.delete(new LambdaQueryWrapper<ActorMovie>().eq(ActorMovie::getMovieId,id));
        bannerMapper.delete(new LambdaQueryWrapper<Banner>().eq(Banner::getMovieId,id));
        ratingMapper.delete(new LambdaQueryWrapper<Rating>().eq(Rating::getMovieId,id));
        reviewMapper.delete(new LambdaQueryWrapper<Review>().eq(Review::getMovieId,id));
        return movieMapper.deleteById(id) > 0;
    }
}




